package cn.bdqn.hospital.dao.impl;

import cn.bdqn.hospital.dao.CheckmDao;
import cn.bdqn.hospital.entity.Checkm;
import cn.bdqn.hospital.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class CheckmDaoImpl implements CheckmDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 查询所有的检查项目
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Checkm> getAll() throws SQLException {
        List<Checkm> checkmList =null;
        String sql="select * from checkm;";
        checkmList =queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Checkm>(Checkm.class));
        return checkmList;
    }

    /**
     * 通过id查询检查项目
     *
     * @param jcId
     * @return
     * @throws SQLException
     */
    @Override
    public Checkm getCheckmByJcId(Integer jcId) throws SQLException {
        Checkm checkm=null;
        String sql="select * from checkm where jcId=？;";
        checkm = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Checkm>(Checkm.class),jcId);
        return checkm;
    }

    /**
     * 通过项目名字查询
     *
     * @param jcName
     * @return
     * @throws SQLException
     */
    @Override
    public Checkm getCheckmByJcName(String jcName) throws SQLException {
        Checkm checkm =null;
        String sql="select * from checkm where jcName=?;";
        checkm=queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Checkm>(Checkm.class),jcName);
        return checkm;
    }

    /**
     * 新增项目
     *
     * @param checkm
     * @return
     * @throws SQLException
     */
    @Override
    public int add(Checkm checkm) throws SQLException {
        int num=0;
        String sql="insert into checkm(jcName) values(?);";
        num=queryRunner.update(DruidUtils.getConnection(),sql,checkm.getJcName());
        return num;
    }

    /**
     * 修改检查项目
     *
     * @param checkm
     * @return
     * @throws SQLException
     */
    @Override
    public int update(Checkm checkm) throws SQLException {
        int num=0;
        String sql="update checkm set jcName='胸片' where jcId=13;";
        num=queryRunner.update(DruidUtils.getConnection(),sql,checkm.getJcId());
        return num;
    }

    /**
     * 通过id删除检查项目
     *
     * @param jcId
     * @return
     * @throws SQLException
     */
    @Override
    public int delete(Integer jcId) throws SQLException {
        int num=0;
        String sql="delete from checkm where jcId=?;";
        num=queryRunner.update(DruidUtils.getConnection(),sql,jcId);
        return num;
    }
}
